<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*
public function index(){
	$_SESSION = array();
	echo '<a href="'.base_url('script/redirect_sina').'">sina</a><br />';
	echo '<a href="'.base_url('script/redirect_tencent').'">tencent</a><br />';
	echo '<a href="'.base_url('script/redirect_netease').'">netease</a><br />';
	echo '<a href="'.base_url('script/redirect_sohu').'">sohu</a><br />';
}

public function redirect_sina(){
	$this->load->library('weibo');
	$this->weibo->setType(Weibo::WEIBO_TYPE_SINA);
	$url = $this->weibo->getAuthorizeURL();
	redirect($url);
}

public function redirect_tencent(){
	$this->load->library('weibo');
	$this->weibo->setType(Weibo::WEIBO_TYPE_TENCENT);
	$url = $this->weibo->getAuthorizeURL();
	redirect($url);
}

public function redirect_netease(){
	$this->load->library('weibo');
	$this->weibo->setType(Weibo::WEIBO_TYPE_NETEASE);
	$url = $this->weibo->getAuthorizeURL();
	redirect($url);
}

public function redirect_sohu(){
	$this->load->library('weibo');
	$this->weibo->setType(Weibo::WEIBO_TYPE_SOHU);
	$url = $this->weibo->getAuthorizeURL();
	redirect($url);
}

public function callback_sina(){
	$this->load->library('weibo');
	$this->weibo->setType(Weibo::WEIBO_TYPE_SINA);
	$this->weibo->link(array(
		'code' => $this->input->get('code'),
	));

	if(!$this->weibo->linked()){
		redirect('script');
	}else{
		redirect('script/getUID');
	}
}

public function callback_tencent(){
	$this->load->library('weibo');
	$this->weibo->setType(Weibo::WEIBO_TYPE_TENCENT);
	$this->weibo->link(array(
		'code' => $this->input->get('code'),
		'openid' => $this->input->get('openid'),
		'openkey' => $this->input->get('openkey'),
	));

	if(!$this->weibo->linked()){
		redirect('script');
	}else{
		redirect('script/getUID');
	}
}

public function callback_netease(){
	$this->load->library('weibo');
	$this->weibo->setType(Weibo::WEIBO_TYPE_NETEASE);
	$this->weibo->link(array(
		'oauth_token' => $this->input->get('oauth_token'),
	));

	if(!$this->weibo->linked()){
		redirect('script');
	}else{
		redirect('script/getUID');
	}
}

public function callback_sohu(){
	$this->load->library('weibo');
	$this->weibo->setType(Weibo::WEIBO_TYPE_SOHU);
	$this->weibo->link(array(
		'oauth_token' => $this->input->get('oauth_token'),
		'oauth_verifier' => $this->input->get('oauth_verifier'),
	));

	if(!$this->weibo->linked()){
		redirect('script');
	}else{
		redirect('script/getUID');
	}
}

public function getUID(){
	$this->load->library('weibo');
	if(!$this->weibo->linked()) redirect('script');
	echo $this->weibo->getUID();
}

public function getFriendList(){
	$this->load->library('weibo');
	if(!$this->weibo->linked()) redirect('script');
	$friend_list = $this->weibo->getFriendList();
	print_r($friend_list);
}

public function sendContent(){
	$this->load->library('weibo');
	if(!$this->weibo->linked()) redirect('script');
	if($this->weibo->sendContent('hello.world')){
		echo 'success';
	}else{
		echo 'failed';
	}
}

public function logout(){
	$this->load->library('weibo');
	$this->weibo->cleanup();
	redirect('script');
}
*/


class Weibo {

	const WEIBO_TYPE_SINA = 1;
	const WEIBO_TYPE_TENCENT = 2;
	const WEIBO_TYPE_NETEASE = 3;
	const WEIBO_TYPE_SOHU = 4;

	const WEIBO_SINA_APPKEY = '1282128220';
	const WEIBO_SINA_APPSECRET = '09b9029247caae3e8d8de32483526956';
	const WEIBO_SINA_CALLBACKURL = 'http://www.mpc.com/script/callback_sina';

	const WEIBO_TENCENT_APPKEY = '801181321';
	const WEIBO_TENCENT_APPSECRET = '61fc990ef4ba01baa035c2bfdfc8ea07';
	const WEIBO_TENCENT_CALLBACKURL = 'http://www.mpc.com/script/callback_tencent';

	const WEIBO_NETEASE_CONSUMERKEY = 'a01sCenGpfh3nhat';
	const WEIBO_NETEASE_CONSUMERSECRET = 'wnmDuPapUTb8wvo10gfapDQIj73MX3lL';
	const WEIBO_NETEASE_CALLBACKURL = 'http://www.mpc.com/script/callback_netease';

	const WEIBO_SOHU_CONSUMERKEY = 'E4UtE6MlKU1frI0rgU9Q';
	const WEIBO_SOHU_CONSUMERSECRET = '16AQz#tOtX11=Zd^nrSslfnHcxfBCA36%kKjBYOB';
	const WEIBO_SOHU_CALLBACKURL = 'http://www.mpc.com/script/callback_sohu';

	protected $_weibo_type = '';

	public function __construct(){
		if(isset($_SESSION['weibo_type']) && in_array($_SESSION['weibo_type'],array(
			Weibo::WEIBO_TYPE_SINA,
			Weibo::WEIBO_TYPE_TENCENT,
			Weibo::WEIBO_TYPE_NETEASE,
			Weibo::WEIBO_TYPE_SOHU,
		))){
			$this->setType($_SESSION['weibo_type']);
		}
	}
/*
| -------------------------------------------------------------------
|  Public Functions
| -------------------------------------------------------------------
*/
	public function setType($weibo_type){
		if($this->_weibo_type == '' && $weibo_type == Weibo::WEIBO_TYPE_SINA){
			include APPPATH.'third_party/sina/saetv2.ex.class.php';
		}elseif($this->_weibo_type == '' && $weibo_type == Weibo::WEIBO_TYPE_TENCENT){
			include APPPATH.'third_party/tencent/Tencent.php';
		}elseif($this->_weibo_type == '' && $weibo_type == Weibo::WEIBO_TYPE_NETEASE){
			include APPPATH.'third_party/netease/tblog.class.php';
		}elseif($this->_weibo_type == '' && $weibo_type == Weibo::WEIBO_TYPE_SOHU){
			include APPPATH.'third_party/sohu/SohuOAuth.php';
		}
		$this->_weibo_type = $weibo_type;
	}

	public function getAuthorizeURL(){
		$url = '';
		if($this->_weibo_type == Weibo::WEIBO_TYPE_SINA){
			$url = $this->_sina_getAuthorizeURL();
		}elseif($this->_weibo_type == Weibo::WEIBO_TYPE_TENCENT){
			$url = $this->_tencent_getAuthorizeURL();
		}elseif($this->_weibo_type == Weibo::WEIBO_TYPE_NETEASE){
			$url = $this->_netease_getAuthorizeURL();
		}elseif($this->_weibo_type == Weibo::WEIBO_TYPE_SOHU){
			$url = $this->_sohu_getAuthorizeURL();
		}

		return $url;
	}

	public function link($params = array()){
		if($this->_weibo_type == Weibo::WEIBO_TYPE_SINA){
			return $this->_sina_link($params);
		}elseif($this->_weibo_type == Weibo::WEIBO_TYPE_TENCENT){
			return $this->_tencent_link($params);
		}elseif($this->_weibo_type == Weibo::WEIBO_TYPE_NETEASE){
			return $this->_netease_link($params);
		}elseif($this->_weibo_type == Weibo::WEIBO_TYPE_SOHU){
			return $this->_sohu_link($params);
		}

		return false;
	}

	public function linked(){
		if($this->_weibo_type == Weibo::WEIBO_TYPE_SINA){
			return $this->_sina_linked();
		}elseif($this->_weibo_type == Weibo::WEIBO_TYPE_TENCENT){
			return $this->_tencent_linked();
		}elseif($this->_weibo_type == Weibo::WEIBO_TYPE_NETEASE){
			return $this->_netease_linked();
		}elseif($this->_weibo_type == Weibo::WEIBO_TYPE_SOHU){
			return $this->_sohu_linked();
		}		

		return false;
	}

	public function getUID(){
		if($this->_weibo_type == Weibo::WEIBO_TYPE_SINA){
			return $this->_sina_getUID();
		}elseif($this->_weibo_type == Weibo::WEIBO_TYPE_TENCENT){
			return $this->_tencent_getUID();
		}elseif($this->_weibo_type == Weibo::WEIBO_TYPE_NETEASE){
			return $this->_netease_getUID();
		}elseif($this->_weibo_type == Weibo::WEIBO_TYPE_SOHU){
			return $this->_sohu_getUID();
		}

		return 0;
	}

	public function getFriendList(){
		if($this->_weibo_type == Weibo::WEIBO_TYPE_SINA){
			return $this->_sina_getFriendList();
		}elseif($this->_weibo_type == Weibo::WEIBO_TYPE_TENCENT){
			return $this->_tencent_getFriendList();
		}elseif($this->_weibo_type == Weibo::WEIBO_TYPE_NETEASE){
			return $this->_netease_getFriendList();
		}elseif($this->_weibo_type == Weibo::WEIBO_TYPE_SOHU){
			return $this->_sohu_getFriendList();
		}

		return array();
	}

	public function sendContent($content){
		if($this->_weibo_type == Weibo::WEIBO_TYPE_SINA){
			return $this->_sina_sendContent($content);
		}elseif($this->_weibo_type == Weibo::WEIBO_TYPE_TENCENT){
			return $this->_tencent_sendContent($content);
		}elseif($this->_weibo_type == Weibo::WEIBO_TYPE_NETEASE){
			return $this->_netease_sendContent($content);
		}elseif($this->_weibo_type == Weibo::WEIBO_TYPE_SOHU){
			return $this->_sohu_sendContent($content);
		}

		return false;
	}

	public function cleanup(){
		if(isset($_SESSION['weibo_type'])) unset($_SESSION['weibo_type']);
		if(isset($_SESSION['t_access_token'])) unset($_SESSION['t_access_token']);
		if(isset($_SESSION['t_openid'])) unset($_SESSION['t_openid']);
		if(isset($_SESSION['t_openkey'])) unset($_SESSION['t_openkey']);
		if(isset($_SESSION['t_oauth_token'])) unset($_SESSION['t_oauth_token']);
		if(isset($_SESSION['t_oauth_token_secret'])) unset($_SESSION['t_oauth_token_secret']);
	}
/*
| -------------------------------------------------------------------
|  SINA Private Functions
| -------------------------------------------------------------------
*/
	protected function _sina_getAuthorizeURL(){
		$oauth = new SaeTOAuthV2(Weibo::WEIBO_SINA_APPKEY,Weibo::WEIBO_SINA_APPSECRET);
		return $oauth->getAuthorizeURL(Weibo::WEIBO_SINA_CALLBACKURL);
	}

	protected function _sina_link($params){
		if(!isset($params['code']) || $params['code'] === false) return false;

		try{
			$oauth = new SaeTOAuthV2(Weibo::WEIBO_SINA_APPKEY,Weibo::WEIBO_SINA_APPSECRET);
			$token = $oauth->getAccessToken('code',array(
				'code' => $params['code'],
				'redirect_uri' => Weibo::WEIBO_SINA_CALLBACKURL,
			));
		} catch (OAuthException $e) {}

		if(!is_array($token)) return false;
		if(!isset($token['access_token'])) return false;

		$_SESSION['weibo_type'] = Weibo::WEIBO_TYPE_SINA;
		$_SESSION['t_access_token'] = $token['access_token'];
		return true;
	}

	protected function _sina_linked(){
		if(!isset($_SESSION['weibo_type']) || $_SESSION['weibo_type'] != Weibo::WEIBO_TYPE_SINA) return false;
		if(!isset($_SESSION['t_access_token'])) return false;

		return true;
	}

	protected function _sina_getUID(){
		if(!$this->_sina_linked()) return 0;

		$client = new SaeTClientV2(Weibo::WEIBO_SINA_APPKEY,Weibo::WEIBO_SINA_APPSECRET,$_SESSION['t_access_token']);
		$uid = $client->get_uid();
		if($uid && isset($uid['uid'])){
			return $uid['uid'];
		}else{
			return 0;
		}
	}

	protected function _sina_getFriendList(){
		if(!$this->_sina_linked()) return array();

		$list = array();
		$client = new SaeTClientV2(Weibo::WEIBO_SINA_APPKEY,Weibo::WEIBO_SINA_APPSECRET,$_SESSION['t_access_token']);

		$uid = $client->get_uid();
		if($uid && isset($uid['uid'])){
			$uid = $uid['uid'];
		}else{
			return $list;
		}

		$i = 0;
		$next_cursor = 0;
		while($i<10){
			$section = $client->friends_by_id($uid,$next_cursor,200);
			if(!is_array($section) || !isset($section['users'])) break;
			foreach($section['users'] as $user){
				$list[] = array(
					'id' => $user['id'],
					'name' => $user['name'],
					'nick' => $user['screen_name'],
					'avatar' => $user['profile_image_url'],
				);
			}
			if($section['next_cursor'] == 0) break;
			$next_cursor = $section['next_cursor'];
			$i++;
		}

		return $list;
	}

	protected function _sina_sendContent($content){
		if(!$this->_sina_linked()) return false;

		$client = new SaeTClientV2(Weibo::WEIBO_SINA_APPKEY,Weibo::WEIBO_SINA_APPSECRET,$_SESSION['t_access_token']);
		$res = $client->update($content);
		if(is_array($res) && !isset($res['error_code'])) return true;
	}
/*
| -------------------------------------------------------------------
|  TENCENT Private Functions
| -------------------------------------------------------------------
*/
	protected function _tencent_getAuthorizeURL(){
		OAuth::init(Weibo::WEIBO_TENCENT_APPKEY,Weibo::WEIBO_TENCENT_APPSECRET);
		return OAuth::getAuthorizeURL(Weibo::WEIBO_TENCENT_CALLBACKURL);
	}

	protected function _tencent_link($params){
		if(!isset($params['code']) || $params['code'] === false) return false;
		if(!isset($params['openid']) || $params['openid'] === false) return false;
		if(!isset($params['openkey']) || $params['openkey'] === false) return false;

		OAuth::init(Weibo::WEIBO_TENCENT_APPKEY,Weibo::WEIBO_TENCENT_APPSECRET);
		$res = Http::request(OAuth::getAccessToken($params['code'],Weibo::WEIBO_TENCENT_CALLBACKURL));
		parse_str($res,$out);
		
		if(!is_array($out)) return false;
		if(!isset($out['access_token'])) return false;

		$_SESSION['weibo_type'] = Weibo::WEIBO_TYPE_TENCENT;
		$_SESSION['t_access_token'] = $out['access_token'];
		$_SESSION['t_openid'] = $params['openid'];
		$_SESSION['t_openkey'] = $params['openkey'];
		return true;
	}

	protected function _tencent_linked(){
		if(!isset($_SESSION['weibo_type']) || $_SESSION['weibo_type'] != Weibo::WEIBO_TYPE_TENCENT) return false;
		if(!isset($_SESSION['t_access_token'])) return false;
		if(!isset($_SESSION['t_openid'])) return false;
		if(!isset($_SESSION['t_openkey'])) return false;
		return true;
	}

	protected function _tencent_getUID(){
		if(!$this->_tencent_linked()) return 0;
		return $_SESSION['t_openid'];
	}

	protected function _tencent_getFriendList(){
		if(!$this->_tencent_linked()) return array();

		OAuth::init(Weibo::WEIBO_TENCENT_APPKEY,Weibo::WEIBO_TENCENT_APPSECRET);
		$i = 0;
		$list = array();
		$next_cursor = 0;
		while($i<10){
			$section = Tencent::api('friends/idollist_s',array('startindex'=>$next_cursor,'reqnum'=>200));
			if(!$section || $section == '') break;
			$section = json_decode($section,true);
			if(!is_array($section) || !isset($section['data']['info'])) break;
			foreach($section['data']['info'] as $user){
				$list[] = array(
					'id' => $user['openid'],
					'name' => $user['name'],
					'nick' => $user['nick'],
					'avatar' => $user['head'].'/180',
				);
			}
			if($section['data']['hasnext'] == 1) break;
			$next_cursor = $section['data']['nextstartpos'];
			$i++;
		}
		return $list;
	}

	protected function _tencent_sendContent($content){
		if(!$this->_tencent_linked()) return false;

		OAuth::init(Weibo::WEIBO_TENCENT_APPKEY,Weibo::WEIBO_TENCENT_APPSECRET);
		$res = Tencent::api('t/add',array('content'=>$content),'POST');
		$res = json_decode($res,true);
		if(is_array($res) && $res['errcode'] == 0) return true;
	}
/*
| -------------------------------------------------------------------
|  NETEASE Private Functions
| -------------------------------------------------------------------
*/
	protected function _netease_getAuthorizeURL(){
		$oauth = new OAuth(Weibo::WEIBO_NETEASE_CONSUMERKEY,Weibo::WEIBO_NETEASE_CONSUMERSECRET);
		$request_token = $oauth->getRequestToken();
		if(!is_array($request_token) || !isset($request_token['oauth_token']) || !isset($request_token['oauth_token_secret'])) return '';
		$_SESSION['t_oauth_token'] = $request_token['oauth_token'];
		$_SESSION['t_oauth_token_secret'] = $request_token['oauth_token_secret'];
		return $oauth->getAuthorizeURL($request_token['oauth_token'],Weibo::WEIBO_NETEASE_CALLBACKURL);
	}

	protected function _netease_link($params){
		if(!isset($params['oauth_token']) || $params['oauth_token'] === false) return false;

		$oauth = new OAuth(Weibo::WEIBO_NETEASE_CONSUMERKEY,Weibo::WEIBO_NETEASE_CONSUMERSECRET,$_SESSION['t_oauth_token'],$_SESSION['t_oauth_token_secret']);
		$access_token = $oauth->getAccessToken($params['oauth_token']);

		if(!is_array($access_token) || !isset($access_token['oauth_token'])) return false;

		$_SESSION['weibo_type'] = Weibo::WEIBO_TYPE_NETEASE;
		$_SESSION['t_oauth_token'] = $access_token['oauth_token'];
		$_SESSION['t_oauth_token_secret'] = $access_token['oauth_token_secret'];
		return true;
	}

	protected function _netease_linked(){
		if(!isset($_SESSION['weibo_type']) || $_SESSION['weibo_type'] != Weibo::WEIBO_TYPE_NETEASE) return false;
		if(!isset($_SESSION['t_oauth_token'])) return false;
		if(!isset($_SESSION['t_oauth_token_secret'])) return false;
		return true;
	}

	protected function _netease_getUID(){
		if(!$this->_netease_linked()) return 0;

		$tblog = new TBlog(Weibo::WEIBO_NETEASE_CONSUMERKEY,Weibo::WEIBO_NETEASE_CONSUMERSECRET,$_SESSION['t_oauth_token'],$_SESSION['t_oauth_token_secret']);
		$res = $tblog->show_user();

		if(is_array($res) && isset($res['id'])) return $res['id'];
		return 0;
	}

	protected function _netease_getFriendList(){
		if(!$this->_netease_linked()) return array();

		$tblog = new TBlog(Weibo::WEIBO_NETEASE_CONSUMERKEY,Weibo::WEIBO_NETEASE_CONSUMERSECRET,$_SESSION['t_oauth_token'],$_SESSION['t_oauth_token_secret']);

		$i = 0;
		$list = array();
		$next_cursor = 0;
		while($i<10){
			$i++;
			$section = $tblog->friends(null,$next_cursor);
			if(!is_array($section) || !isset($section['users'])) break;
			foreach($section['users'] as $user){
				$list[] = array(
					'id' => $user['id'],
					'name' => $user['name'],
					'nick' => $user['name'],
					'avatar' => $user['profile_image_url'],
				);
			}
			if($section['next_cursor'] == -1) break;
			$next_cursor = $section['next_cursor'];
		}
		return $list;
	}

	protected function _netease_sendContent($content){
		if(!$this->_netease_linked()) return false;

		$tblog = new TBlog(Weibo::WEIBO_NETEASE_CONSUMERKEY,Weibo::WEIBO_NETEASE_CONSUMERSECRET,$_SESSION['t_oauth_token'],$_SESSION['t_oauth_token_secret']);
		$res = $tblog->update($content);
		if(is_array($res) && !isset($res['error_code'])) return true;
	}
/*
| -------------------------------------------------------------------
|  SOHU Private Functions
| -------------------------------------------------------------------
*/
	protected function _sohu_getAuthorizeURL(){
		$oauth = new SohuOAuth(Weibo::WEIBO_SOHU_CONSUMERKEY,Weibo::WEIBO_SOHU_CONSUMERSECRET);
		$request_token = $oauth->getRequestToken(Weibo::WEIBO_SOHU_CALLBACKURL);
		if(!is_array($request_token) || !isset($request_token['oauth_token']) || !isset($request_token['oauth_token_secret'])) return '';
		$_SESSION['t_oauth_token'] = $request_token['oauth_token'];
		$_SESSION['t_oauth_token_secret'] = $request_token['oauth_token_secret'];
		return $oauth->getAuthorizeUrl1($request_token['oauth_token'],Weibo::WEIBO_SOHU_CALLBACKURL);
	}

	protected function _sohu_link($params){
		if(!isset($params['oauth_token']) || $params['oauth_token'] === false) return false;
		if(!isset($params['oauth_verifier']) || $params['oauth_verifier'] === false) return false;

		$oauth = new SohuOAuth(Weibo::WEIBO_SOHU_CONSUMERKEY,Weibo::WEIBO_SOHU_CONSUMERSECRET,$_SESSION['t_oauth_token'],$_SESSION['t_oauth_token_secret']);
		$access_token = $oauth->getAccessToken($params['oauth_verifier']);

		if(!is_array($access_token) || !isset($access_token['oauth_token'])) return false;

		$_SESSION['weibo_type'] = Weibo::WEIBO_TYPE_SOHU;
		$_SESSION['t_oauth_token'] = $access_token['oauth_token'];
		$_SESSION['t_oauth_token_secret'] = $access_token['oauth_token_secret'];
		return true;
	}

	protected function _sohu_linked(){
		if(!isset($_SESSION['weibo_type']) || $_SESSION['weibo_type'] != Weibo::WEIBO_TYPE_SOHU) return false;
		if(!isset($_SESSION['t_oauth_token'])) return false;
		if(!isset($_SESSION['t_oauth_token_secret'])) return false;
		return true;
	}

	protected function _sohu_getUID(){
		if(!$this->_sohu_linked()) return 0;

		$oauth = new SohuOAuth(Weibo::WEIBO_SOHU_CONSUMERKEY,Weibo::WEIBO_SOHU_CONSUMERSECRET,$_SESSION['t_oauth_token'],$_SESSION['t_oauth_token_secret']);
		$res = $oauth->get('http://api.t.sohu.com/users/show.json');
		$res = (array)$res;

		if(is_array($res) && isset($res['id'])) return $res['id'];
		return 0;
	}

	protected function _sohu_getFriendList(){
		if(!$this->_sohu_linked()) return array();

		$oauth = new SohuOAuth(Weibo::WEIBO_SOHU_CONSUMERKEY,Weibo::WEIBO_SOHU_CONSUMERSECRET,$_SESSION['t_oauth_token'],$_SESSION['t_oauth_token_secret']);

		$i = 0;
		$list = array();
		while($i<10){
			$i++;
			$section = $oauth->get('http://api.t.sohu.com/statuses/friends.json?page='.$i);
			if(!is_array($section)) break;
			foreach($section as $user){
				$user = (array)$user;
				$list[] = array(
					'id' => $user['id'],
					'name' => $user['screen_name'],
					'nick' => $user['screen_name'],
					'avatar' => $user['profile_image_url'],
				);
			}
		}
		return $list;
	}

	protected function _sohu_sendContent($content){
		if(!$this->_sohu_linked()) return false;

		$oauth = new SohuOAuth(Weibo::WEIBO_SOHU_CONSUMERKEY,Weibo::WEIBO_SOHU_CONSUMERSECRET,$_SESSION['t_oauth_token'],$_SESSION['t_oauth_token_secret']);
		$res = $oauth->post('http://api.t.sohu.com/statuses/update.json',array('status'=>$content));
		$res = (array)$res;
		if(!isset($res['code'])) return true;
	}
}

/* End of file weibo.php */
/* Location: ./application/libraries/weibo.php */